﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class MenuLinkRoleApp : IMenuLinkRole
    {
        private string dbname = "Systems";

        public void Add(MenuLinkRoleEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_MenuLinkRole(");
            builder.Append("RoleId,ChainNum,MenuLinkValue");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.RoleId + ",");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.MenuLinkValue + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int MenuLinkId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_MenuLinkRole ");
            builder.Append(" where MenuLinkId=" + MenuLinkId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_MenuLinkRole ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by MenuLinkId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "MenuLinkId", "T_MenuLinkRole", swhere);
        }

        public MenuLinkRoleEnt GetModel(int MenuLinkId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MenuLinkId,RoleId,ChainNum,MenuLinkValue from T_MenuLinkRole ");
            builder.Append(" where MenuLinkId=" + MenuLinkId);
            MenuLinkRoleEnt ent = new MenuLinkRoleEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.MenuLinkId = MenuLinkId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["RoleId"].ToString() != "")
                {
                    ent.RoleId = int.Parse(table.Rows[0]["RoleId"].ToString());
                }
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.MenuLinkValue = table.Rows[0]["MenuLinkValue"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_MenuLinkRole ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "MenuLinkId");
        }

        public void Update(MenuLinkRoleEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_MenuLinkRole set ");
            builder.Append("RoleId=" + model.RoleId + ",");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("MenuLinkValue='" + model.MenuLinkValue + "'");
            builder.Append(" where MenuLinkId=" + model.MenuLinkId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

